package org.flower.web.servlet;

import org.flower.bean.Order;
import org.flower.bean.OrderState;
import org.flower.service.OrderService;
import org.flower.service.OrderStateService;
import org.flower.service.impl.OrderServiceImpl;
import org.flower.service.impl.OrderStateServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 订单的详细信息有关的servlet
 *
 * @author Aizen
 * @date 2021/12/25
 */
@WebServlet("/manager/orderDetailServlet.shtml")
public class OrderDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService os = new OrderServiceImpl();
        //拿到订单编号
        String id = request.getParameter("id");
        //根据订单编号获取订单信息
        Order order = os.findOrderDetailById(id);
        request.setAttribute("order",order);

        //获取状态表
        OrderStateService oss = new OrderStateServiceImpl();
        List<OrderState> stateList = oss.findAllState();
        request.setAttribute("stateList",stateList);


        //判断去哪个页面
        String u = request.getParameter("u");
        if(u!=null&&u.equals("true")){
            request.getRequestDispatcher("orderDetailUpdate.jsp").forward(request,response);
            return;
        }
        request.getRequestDispatcher("orderDetail.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String servicedate = request.getParameter("servicedate");
        String msg = request.getParameter("msg");
        String ps = request.getParameter("ps");
        Order o = new Order();
        o.setId(id);
        o.setServicedateStr(servicedate);
        o.setGreetingcardmsg(msg);
        o.setContent(ps);

        OrderService os = new OrderServiceImpl();
        boolean b = os.updateOrder(o);
        if(b){
            response.getWriter().write(
                    "<script type=\"text/javascript\">alert('修改成功！');setInterval(\"\",10);parent.location.href='" +
                            request.getContextPath()
                            + "/manager/orderDetailServlet.shtml?id="+o.getId()+"'</script>");
            response.getWriter().flush();
            response.getWriter().close();
        }else{
            response.getWriter().write(
                    "<script type=\"text/javascript\">alert('修改失败！！');setInterval(\"\",10);parent.location.href='" +
                            request.getContextPath()
                            + "/manager/orderDetailServlet.shtml?id="+o.getId()+"'</script>");
            response.getWriter().flush();
            response.getWriter().close();
        }
    }
}
